<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <!-- <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除</el-button> -->
                <el-button
                    type="primary"
                    icon="el-icon-download"
                    @click="downExecl"
                >导出</el-button>
              <el-input v-model="query.search" placeholder="订单号" class="handle-input mr10"></el-input>
              <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="username" label="用户名"></el-table-column>

                <el-table-column prop="orderNo" label="订单号"></el-table-column>
                <el-table-column prop="productName" label="商品名称"></el-table-column>
                <el-table-column prop="createTime" label="支付时间"></el-table-column>
                <el-table-column prop="totalPrice" label="支付价格"></el-table-column>
                <el-table-column label="商品图片" align="center">
                    <template slot-scope="scope">
                        <el-image
                            class="table-td-thumb"
                            :src="scope.row.imgurl"
                            :preview-src-list="[scope.row.imgurl]"
                        ></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="状态" align="center">
                    <template slot-scope="scope">
                        <el-tag
                            :type="scope.row.status== 0?'danger':'success'"
                         >{{scope.row.statusDesc}}</el-tag>
                    </template>
                </el-table-column>

                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <div v-if="scope.row.status==1">
                        <el-button
                            type="text"
                            icon="el-icon-tickets"
                            @click="handleEdit(scope.$index, scope.row)"
                        >详情</el-button>
                        <!-- <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button> -->
                        <el-button
                            type="text"
                            icon="el-icon-truck"
                            @click="handleSend(scope.$index, scope.row)"
                        >发货</el-button>
                        </div>
                        		  <span v-if="scope.row.status==2" icon="el-icon-check" style="color:#67C23A">发货成功</span>


                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
        </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="详情" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="用户名">
                    <el-input v-model="form.username" disabled></el-input>
                </el-form-item>
                <el-form-item label="订单号">
                    <el-input v-model="form.orderNo" disabled></el-input>
                </el-form-item>
                <el-form-item label="商品名称">
                    <el-input v-model="form.productName" disabled></el-input>
                </el-form-item>
                <el-form-item label="支付状态">
                    <el-input v-model="form.status" disabled></el-input>
                </el-form-item>
                <el-form-item label="支付时间">
                    <el-input v-model="form.createTime" disabled></el-input>
                </el-form-item>
                <el-form-item label="支付价格">
                    <el-input v-model="form.totalPrice" disabled></el-input>
                </el-form-item>



            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { fetchData } from '../../api/index';
import { getUsers } from '../../api';
import request from '@/utils/request';
import qee from "qf-export-excel"
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                search: '',
                pageNum: 1,
                pageSize: 8
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            formLabelWidth: '120px',
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            request.get("http://localhost:8081/order/getOrders",{
               params:this.query
        }).then(res=>{
            console.log(res)
            this.tableData = res.data
        })
        },
        // 触发搜索按钮
        handleSearch() {
           this.getData()
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData(); 
                    form.append("orderNo",row.orderNo)
                    request.post("http://localhost:8081/order/del",form).then(res=>{
                        this.$message.success('删除成功');
                        this.getData()
                    })
                    
                    
                })
                .catch(() => {});
        },
        rec(orderNo){
          // 还原
            this.$confirm('确定要还原吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    let form = new FormData(); 
                    form.append("orderNo",orderNo)
                    request.post("http://localhost:8081/order/rec",form).then(res=>{
                        this.$message.success('还原成功');
                        this.getData()
                    })
                    
                    
                })
                .catch(() => {});      
        },      
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            const length = this.multipleSelection.length;
            let str = '';
            this.delList = this.delList.concat(this.multipleSelection);
            const orderNos = []
            this.delList.forEach(item=>{
                orderNos.push(item.orderNo)
            })
            request.post("http://localhost:8081/order/mulDel",this.qs.stringify({ "orderNos":orderNos}, { arrayFormat: 'repeat' })).then(res=>{
                        this.$message.success('删除成功');
                        this.getData()
                    })
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = row;
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
           this.editVisible = false;
           this.getData() 
                        
        },
        handleSend(index,row){
           request.get("http://localhost:8081/order/sendGood",{
               params:{
                   orderNo:row.orderNo
               }
        }).then(res=>{
            this.$message.success('发货成功！！！');
            this.getData()
        })
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        },
        downExecl(){
            const titleList = [
			{ title:'用户名',key:'username'},
			{ title:'订单号',key:'orderNo'},
			{ title:'商品名称',key:'productName'},
			{ title:'支付时间',key:'createTime'},
			{ title:'支付价格',key:'totalPrice'},
		]
		qee(titleList,this.tableData,'orders')
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
    margin-left: 20px;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.yellow{
    color: rgb(136, 138, 18);
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
